<?php

class DocumentationParser
{
    public static function savePageInfo()
    {
    
    }

    public static function parseFile($project_id, $file)
    {        
        $xmlTest = simplexml_load_file($file);

		ManyInsertQueryBuilder::$queryLimit = 50;
        if ($xmlTest) {
            $fileInfo = $xmlTest->parent;
            
            $logger = Logger::getLogger( 'DocumentationParser' );
            $package = DocPackage::saveIfNotExists(array('name'=>$fileInfo->package, 'subpackage'=>$fileInfo->subpackage));
            /*if(empty($fileInfo->package) && empty($fileInfo->subpackage)) {
                $logger->debug($package->id.' empty name and subpackage in file '.$file);
            }*/
            $path = rtrim( dirname( $fileInfo->location ), DIRECTORY_SEPARATOR );
            $pfolder = Pfolder::saveIfNotExists(array('name'=>$path,'path'=>$path,'project_id'=>$project_id));                         
            
			$pfile = new Pfile();
			$pfile->name = $fileInfo->file;
			$pfile->location = $fileInfo->location;
			$pfile->has_interfaces = $xmlTest->hasInterfaces;
			$pfile->has_classes = $xmlTest->hasClasses;
			$pfile->folder_id = $pfolder->id;
			$pfile->package_id = $package->id;
			$pfile->source = $fileInfo->source;
			$pfile->save();
            
            foreach((array)$xmlTest->classes as $class) {                
                if(empty($class->docblock->package) && empty($class->docblock->subpackage)) {
                    $package = DocPackage::saveIfNotExists(array('name'=>$fileInfo->package, 'subpackage'=>$fileInfo->subpackage));                    
                    //$logger->debug($package->id.' empty name and subpackage in file '.$file);
                } else {
                    $package = DocPackage::saveIfNotExists(array('name'=>$class->docblock->package, 'subpackage'=>$class->docblock->subpackage));
                }
				
				$doctype = new Doctype();
				$doctype->category = $class->docblock->category;
				$doctype->package_id = $package->id;
				$doctype->shortdescription = $class->docblock->sdesc;
				$doctype->description = $class->docblock->desc;
				$doctype->start_number = $class->docblock->linenumber;
				$doctype->end_number = $class->docblock->endlinenumber;
				$doctype->save();
                
				$dtData = array();
                foreach((array)$class->docblock->tags as $tag) {
                    $res = null;
                    $tv = (array)$tag->value;
                    if(array_key_exists('el_0',$tv)) {
                        $res = $tv['el_0'];
                        if($res instanceof SimpleXMLElement) {
                            $res = (array)$res;
                        }
                    } else {
                        $res = $tv;
                    }
                    if(count($res) == 0) {
                        $value = '';
                    } elseif( is_array($res) && array_key_exists('value',$res) ) {
                        $value = self::generateHtml($res['value']);
                    } else {
                        $value = $res;
                    }
                    
                    $type = '';
                    if( array_key_exists('type',$res) ) {
                        $type = $res['type'];
                    }

					$item = array();
					$item['name'] = $tag['keyword'];
					$item['type'] = $type;
					$item['value'] = $value;
					$item['doctype_id'] = $doctype->id;
					$dtData[] = $item;
                }	
				Doctags::manyInsert( $dtData, 'Doctags' );
                
				$pclass = new Phpclass();
				$pclass->name = $class->name;
				$pclass->extends = $class->extends;
				$pclass->implements = implode(',',(array)$class->implements);
				$pclass->start_number = $class->linenumber;
				$pclass->end_number = $class->endlinenumber;
				$pclass->is_abstract = ($class->isAbstract == 'true') ? true : false;
				$pclass->is_interface = ($class->isInterface == 'true') ? true : false;
				$pclass->doctype_id = $doctype->id;
				$pclass->file_id = $pfile->id;
				$pclass->save();
				
				
				foreach((array)$class->methods as $method) {
					$methsDoctype = new Doctype();
					$methsDoctype->category = $class->docblock->category;
					$methsDoctype->package_id = $package->id;
					$methsDoctype->shortdescription = $method->docblock->sdesc;
					$methsDoctype->description = $method->docblock->desc;
					$methsDoctype->start_number = $method->docblock->linenumber;
					$methsDoctype->end_number = $method->docblock->endlinenumber;
					$methsDoctype->save();

					$dtData = array();                
					foreach((array)$method->docblock->params as $param) {
						$item = array();
						$item['name'] = 'param';
						$item['type'] = '';
						$item['value'] = $param->var;
						$item['doctype_id'] = $methsDoctype->id;
						$dtData[] = $item;
					}	
					Doctags::manyInsert( $dtData, 'Doctags' );
					
					$access = 'public';
					$is_abstract = false;
					foreach((array)$method->modifiers as $modify) {
						if($modify == 'abstract') {
							$is_abstract = true;
						} else {
							$access = $modify;
						}
					}
					
					$pfunction = new Phpfunction();
					$pfunction->name = $method->name;
					$pfunction->access = $access;
					$pfunction->start_number = $method->linenumber;
					$pfunction->end_number = $method->endlinenumber;
					$pfunction->is_abstract = $is_abstract;
					$pfunction->is_constructor = ($method->isConstructor == 'true') ? true : false;
					$pfunction->is_destructor = ($method->isDestructor == 'true') ? true : false;
					$pfunction->phpclass_id = $pclass->id;
					$pfunction->doctype_id = $methsDoctype->id;
					$pfunction->save();
				}				
            }
			
			
        } else {
            $logger = Logger::getLogger( 'DocumentationParser' );
            $logger->debug('not found xml in file '.$file);
        }
    }
    
    public static function generateHtml($xmlNode)
    {
        $html = '';
        foreach($xmlNode as $vals) {
            $vals = (array)$vals;
            if( array_key_exists('type',$vals) ) {
                if($vals['type'] == 'inlinetag' && $vals['inlinetype'] == 'link') {
                    $html .= '<a href="'.$vals['value'].'">'.$vals['linktext'].'</a>';
                }
            } else {
                $html .= $vals[0];
            }
        }
        return $html;
    }
}